package com.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.app.mapper.jiuquan.ComplaintMapper;
import com.app.model.ComplainModel;
import com.app.service.ComplaintService;
import com.app.util.JsonUtils;
import com.app.util.StringTool;
import com.app.vo.ComplainReturnVo;
import com.app.vo.ComplaintVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, ComplainModel>implements ComplaintService {

    @Autowired
    ComplaintMapper complaintMapper;

    @Override
    public ComplainModel getModelByAreaIdAndCode(int areaId, String code) {
        QueryWrapper<ComplainModel> qw = new QueryWrapper<>();
        qw.eq("area_id", areaId);
        qw.eq("area_code", code);
        ComplainModel model = complaintMapper.selectOne(qw);
        return model;
    }

    @Override
    public ComplainModel getModelByCode(String code) {
        QueryWrapper<ComplainModel> qw = new QueryWrapper<>();
        qw.eq("area_code", code);
        ComplainModel model = complaintMapper.selectOne(qw);
        return model;
    }

    @Override
    public ComplainReturnVo getList(ComplainModel model) {
        Page<ComplainModel> page1 = new Page<>(model.getPage(), model.getLimit());
        QueryWrapper<ComplainModel> qw = new QueryWrapper<>();
        if(model.getUserId()!=null&&model.getUserId()>0)
        {
            qw.eq("user_id",model.getUserId());
        }
        qw.orderByDesc("id");
        Page<ComplainModel> incubatorsPage = complaintMapper.selectPage(page1, qw);
        List<ComplainModel> list = incubatorsPage.getRecords();
        ComplainReturnVo vo = new ComplainReturnVo();
        vo.setCount((int) incubatorsPage.getTotal());
        List<ComplaintVo> complaintVoList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = JSONObject.parseObject(list.get(i).getDataJson());
            ComplaintVo complaintVo = JsonUtils.string2Obj(String.valueOf(jsonObject), ComplaintVo.class);
            complaintVo.setId(list.get(i).getId());
            if(StringTool.isNotEmpty(complaintVo.getCreateTime()))
            {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式化样式
                String time1 = sdf.format(new Date(Long.parseLong(complaintVo.getCreateTime())));
                complaintVo.setCreateTime(time1);
            }
            complaintVoList.add(complaintVo);
        }
        vo.setList(complaintVoList);
        return vo;
    }

    @Override
    public ComplaintVo getModelById(Integer id) {
        ComplainModel model = complaintMapper.selectById(id);
        JSONObject jsonObject = JSONObject.parseObject(model.getDataJson());
        ComplaintVo complaintVo = JsonUtils.string2Obj(String.valueOf(jsonObject), ComplaintVo.class);
        return complaintVo;
    }

}
